package quanlyhochieu.tongnuocden;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import utils.DateUtils;
import dbutil.ConnectDB;


public class NuocDen {
	private String error;
	private java.util.Date ngayBatDau;
	private java.util.Date ngayKetThuc;
	private List<Detail> listNuocDen;
	private boolean hienThiListNuocDen = true;
	private int tongSoLuong;
	private String pathToListNuocDen ="/qlhochieu/tongsonuocden/empty.xhtml";
	
	public NuocDen(){
		
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public java.util.Date getNgayBatDau() {
		return ngayBatDau;
	}

	public void setNgayBatDau(java.util.Date ngayBatDau) {
		this.ngayBatDau = ngayBatDau;
	}

	public java.util.Date getNgayKetThuc() {
		return ngayKetThuc;
	}

	public void setNgayKetThuc(java.util.Date ngayKetThuc) {
		this.ngayKetThuc = ngayKetThuc;
	}

	public List<Detail> getListNuocDen() {
		return listNuocDen;
	}

	public void setListNuocDen(List<Detail> listNuocDen) {
		this.listNuocDen = listNuocDen;
	}

	public boolean isHienThiListNuocDen() {
		return hienThiListNuocDen;
	}

	public void setHienThiListNuocDen(boolean hienThiListNuocDen) {
		this.hienThiListNuocDen = hienThiListNuocDen;
	}

	public int getTongSoLuong() {
		return tongSoLuong;
	}

	public void setTongSoLuong(int tongSoLuong) {
		this.tongSoLuong = tongSoLuong;
	}

	public String reset(){		
		this.ngayBatDau = null;
		this.ngayKetThuc = null;
		this.listNuocDen = null;
		this.setError("");
		return "thongkenuocden";
	}
	 public String thongKeSoLuongNuocDen(){
		this.setListNuocDen(this.chooseListNuocDen());
		return "thongkenuocden";
	}
	 public int getNumberMember(String maDoan){
		 String sql = "select * from dsnguoidi where iddoanra = '"+maDoan+"'";
		 ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(sql);
			return array.size();
	 }
	public  List<Detail> chooseListNuocDen(){
		List<Detail> list = new ArrayList<Detail>();
		String SQL = "select * from doanra as DR, mucdich as MD where DR.idmucdich = MD.id and DR.flagdelete = 0";
		SQL+="and ngaydi > '"+DateUtils.utilDateToSqlDate(this.ngayBatDau)+"' ";
		SQL+="and ngayve < '"+DateUtils.utilDateToSqlDate(this.ngayKetThuc)+"'";
		System.out.println("SQL "+SQL);
		ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(SQL);
		this.setTongSoLuong(array.size());
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			int stt = 0;
			for (int i = 0; i < array.size(); i++) {
				stt = i + 1;
				hash = (HashMap<String, Object>) array.get(i);
				Detail detail = new Detail();
				String maDoan = hash.get("madoan").toString();
				detail.setStt(stt);
				detail.setDonViMoi(hash.get("donvimoi").toString());
				detail.setMaDoan(maDoan);
				detail.setMucDich(hash.get("mucdich").toString());
				detail.setNgayDi(DateUtils.formatDate(hash.get("ngaydi").toString()));
				detail.setNgayVe(DateUtils.formatDate(hash.get("ngayve").toString()));
				detail.setNuocDen(hash.get("nuocden").toString());
				detail.setKinhPhi(hash.get("chiphi").toString());
				detail.setSoThanhVien(this.getNumberMember(maDoan));
				list.add(detail);
			}
		}
		return list;
	}

	public String getPathToListNuocDen() {
		if(this.hienThiListNuocDen == false) this.pathToListNuocDen = "/qlhochieu/tongsonuocden/empty.xhtml";
		else this.pathToListNuocDen = "/qlhochieu/tongsonuocden/listNuocDen.xhtml";
		return pathToListNuocDen;
		}

	public void setPathToListNuocDen(String pathToListNuocDen) {
		this.pathToListNuocDen = pathToListNuocDen;
	}
}
